//
//  YeeSignVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/14.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeSignVC.h"
#import "XRCommonOnlyTextFieldTableCell.h"
#import "XRCommonOnlyButtontableCell.h"
#import "YeeDataRequestManger.h"
@interface YeeSignVC ()
{
   UITextField   *_phoneTextField;//手机号码
   UIButton      *_rightButton;
    
}
@end

@implementation YeeSignVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)addOwnViews
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UILabel  *lable=[[UILabel alloc] init];
    lable.text=@"请输入你的手机号码";
    lable.textColor=[UIColor blackColor];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font=[UIFont systemFontOfSize:16];
    [lable setFrame:CGRectMake(0, 50, kMainScreenWidth, 21)];
    [self.view addSubview:lable];
    
    _phoneTextField=[[UITextField alloc] init];
    _phoneTextField.placeholder=@"请输入手机号码";
    _phoneTextField.font=[UIFont systemFontOfSize:14];
    [_phoneTextField setFrame:CGRectMake(20, 50+30, kMainScreenWidth-40, 37)];
    [_phoneTextField setBackgroundColor:xBackgroundColor];
    _phoneTextField.layer.masksToBounds=YES;
    _phoneTextField.layer.cornerRadius=17.5;
    _phoneTextField.keyboardType=UIKeyboardTypeNumberPad;
    UILabel *textLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 21)];
    textLable.textAlignment=NSTextAlignmentCenter;
    textLable.font=[UIFont systemFontOfSize:13];
    textLable.text=@"+86";
    _phoneTextField.leftSpaceView=textLable;
    [self.view addSubview:_phoneTextField];
    
}
-(void)addNavbarView
{
    self.title=@"注册";
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,60, 30)];
    [rightBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kGrayColor forState:UIControlStateDisabled];
    [rightBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
     rightBtn.titleLabel.font =[UIFont Y_systemFontOfSize:14];
    rightBtn.enabled=NO;
//    [rightBtn setBackgroundImage:[UIImage imageWithColor:kGrayColor] forState:UIControlStateDisabled];
//    [rightBtn setBackgroundImage:[UIImage imageWithColor:kBlackColor] forState:UIControlStateNormal];
    _rightButton=rightBtn;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)configOwnViews
{
    
    
}
-(void)bindViewModel
{
    RAC(_rightButton,enabled)=[_phoneTextField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value)
    {
        RACSignal  *signal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber)
        {
            [subscriber sendNext:@([NSString isMobileNumber:value])];
            [subscriber sendCompleted];
            return nil;
        }];
        return signal;
    }];
    
    @weakify(self);
    [[_rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x)
    {
        @strongify(self);
        YeeSignSecondVC  *newVC=[[YeeSignSecondVC alloc] init];
        newVC.jobName=@"12344";
        newVC.phone=_phoneTextField.text;
        [self.navigationController pushViewController:newVC animated:YES];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

@interface YeeSignSecondVC ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *textAcc;//验证码
@property (nonatomic, strong) UITextField *pwdTextField;//输入密码
@property (nonatomic, strong) UITextField *secondpwdTextField;//输入密码

@property (nonatomic, strong) UITextField *YQTextField;//邀请码

@property (nonatomic, strong) UIButton  *Btn;//验证码
@property (nonatomic, strong) UIButton  *signUpButton;//注册按钮

@property(nonatomic, assign)  NSInteger count;
@property(nonatomic, assign)  BOOL isFirst;//用于判断是否是第一次自动倒计时
@end

@implementation YeeSignSecondVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"注册账号";
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self addTableViewHeaderView];
}
-(void)configOwnViews
{
    [super configOwnViews];
    [self.view setBackgroundColor:xBackgroundColor];
    self.tableView.separatorColor=[UIColor clearColor];
}
-(void)bindViewModel
{

    
    
//  //信号集合操作
//    RAC(self.signUpButton,enabled)=[RACSignal combineLatest:@[_textAcc.rac_textSignal,_pwdTextField.rac_textSignal,_secondpwdTextField.rac_textSignal] reduce:^id (NSString * ystring,NSString *pwdString,NSString *secondString)
//    {
//        return @(ystring.length>=4&&pwdString.length>=6&&secondString==pwdString);
//    }];
    
    
}
-(void)addTableViewHeaderView
{
    UIView *headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 120)];
    UILabel  *lable=[[UILabel alloc] init];
    lable.text=@"验证您的手机号并注册";
    lable.textColor=[UIColor blackColor];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font=[UIFont systemFontOfSize:15];
    [lable setFrame:CGRectMake(0, 20,kMainScreenWidth, 21)];
    [headerView addSubview:lable];
    
    UILabel  *phoneLable=[[UILabel alloc] init];
    phoneLable.text=_phone;
    phoneLable.textColor=[UIColor blackColor];
    phoneLable.textAlignment=NSTextAlignmentCenter;
    phoneLable.font=[UIFont systemFontOfSize:25];
    [phoneLable setFrame:CGRectMake(0, 90, kMainScreenWidth, 21)];
    [headerView addSubview:phoneLable];
    self.tableView.tableHeaderView=headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==1||indexPath.section==2||indexPath.section==3)
    {
        XRCommonOnlyTextFieldTableCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XRCommonOnlyTextFieldTableCell class])];
        if (!cell)
        {
            cell=[[XRCommonOnlyTextFieldTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XRCommonOnlyTextFieldTableCell class])];
        }
        cell.textField.font=[UIFont systemFontOfSize:13];
        [cell.textField setBackgroundColor:xBackgroundColor];
        cell.textFieldleftMagin=10.0;
        [cell setBackgroundColor:[UIColor clearColor]];
        if (indexPath.section==1)
        {
            _pwdTextField=cell.textField;
            cell.textField.placeholder=@"请输入密码";
            cell.textField.returnKeyType=UIReturnKeyDone;
            cell.textField.secureTextEntry=YES;
        }else if(indexPath.section==2)
        {
            _secondpwdTextField=cell.textField;
            cell.textField.placeholder=@"请再次输入密码";
            cell.textField.returnKeyType=UIReturnKeyDone;
            cell.textField.secureTextEntry=YES;
        }
        else
        {
            _YQTextField=cell.textField;
            cell.textField.placeholder=@"请输入邀请码(非必填)";
            cell.textField.returnKeyType=UIReturnKeyDone;
            cell.textField.secureTextEntry=YES;
            
        }
        return cell;
    }else if (indexPath.section==0)
    {
        XRCommonOnlyTextFieldWithRightButtonTableCell  *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XRCommonOnlyTextFieldWithRightButtonTableCell class])];
        if (!cell)
        {
            cell=[[XRCommonOnlyTextFieldWithRightButtonTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XRCommonOnlyTextFieldWithRightButtonTableCell class])];
        }
        cell.textField.font=[UIFont systemFontOfSize:13];
        cell.textFieldleftMagin=10.0;
        [cell.textField setBackgroundColor:xBackgroundColor];
        [cell.sendButton setBackgroundColor:kBlackColor];
        [cell.sendButton addTarget:self action:@selector(goToSignUp:) forControlEvents:UIControlEventTouchUpInside];
        cell.textField.placeholder=@"短信验证码";
        _textAcc=cell.textField;
       // [self goToSignUp:cell.sendButton];//第一次自动倒计时
        [cell setBackgroundColor:[UIColor clearColor]];
        return cell;
    }else
    {
        XRCommonOnlyButtontableCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XRCommonOnlyButtontableCell class])];
        if (!cell)
        {
            cell=[[XRCommonOnlyButtontableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XRCommonOnlyButtontableCell class])];
        }
        [cell.contentButton setBackgroundColor:kNavBarThemeColor];
        [cell.contentButton setTitle:@"注册" forState:UIControlStateNormal];
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.buttonBlock=^(void)
        {
            [self nextSignUp];//注册
        };
        return cell;
    }
    return nil;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    return view;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 0.01)];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section==0?0.01:20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark 发送验证码
- (void)goToSignUp:(UIButton *)sender
{
    
   
}
#pragma mark --private Method-提交数据
-(void)nextSignUp
{
//    [self.view endEditing:YES];//
//    if ([NSString MyStringIsNULL:self.textAcc.text])
//    {   [self showMBProgressHUDErrorMessage:@"请输入验证码" inView:self.view.window];
//        return;
//    }else if ([ NSString MyStringIsNULL:self.pwdTextField.text])
//    {   [self showMBProgressHUDErrorMessage:@"请输入密码" inView:self.view.window];
//        return;
//    }else if ([NSString MyStringIsNULL:self.secondpwdTextField.text])
//    {  [self showMBProgressHUDErrorMessage:@"请再次输入密码" inView:self.view.window];
//        return;
//    } if (![self.secondpwdTextField.text  isEqualToString:self.pwdTextField.text])
//    {  [self showMBProgressHUDErrorMessage:@"两次输入的密码不同" inView:self.view.window];
//        return;
//    }
   
}
-(void)makeTimer:(UIButton *)sender
{
    _count = 60;
    sender.enabled  = NO;
    [sender setTitle:@"60秒" forState:(UIControlStateNormal)];
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (_count != 1) {
            _count -= 1;
            dispatch_async(dispatch_get_main_queue(), ^{
                [sender setTitle:[NSString stringWithFormat:@"%ld秒",(long)_count] forState:(UIControlStateNormal)];
            });
            
        }else if (_count == 1){
            sender.enabled  = YES;
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [sender setTitle:@"获取验证码" forState:(UIControlStateNormal)];
                
            });
        }
    });
    dispatch_resume(timer);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end

